{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "toc_visible": true,
      "authorship_tag": "ABX9TyO7Idjomnu0KJYEWjWHVBb7",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/CrazyBoyM/llama3-Chinese-chat/blob/main/tools/transfer_hf_models_to_modelscope.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "9T4NXb4DYL5-"
      },
      "outputs": [],
      "source": [
        "# 安装需要的包\n",
        "! pip install -U gradio huggingface-hub modelscope"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 登录Huggingface的token，获取地址：https://huggingface.co/settings/tokens\n",
        "! huggingface-cli login --token \"请输入token\""
      ],
      "metadata": {
        "id": "QixnAi5wYpTy"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# 查看磁盘空间是否足够用于下载大模型\n",
        "! df -h"
      ],
      "metadata": {
        "id": "CAHpmzAlaW1f"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# 0. 设置需要下载的模型的名称和本地路径\n",
        "model_id = \"shareAI/CodeLLaMA-chat-13b-Chinese\" # 在hf的模型id，请修改为你自己想下载的模型\n",
        "local_model_dir = f\"/hf-models/{model_id}\" # 下载本地后存储位置\n",
        "! echo $local_model_dir"
      ],
      "metadata": {
        "id": "kUr71vasaOZ6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# 1. 从huggingface下载模型\n",
        "\n",
        "! huggingface-cli download --resume-download $model_id --local-dir $local_model_dir --local-dir-use-symlinks False"
      ],
      "metadata": {
        "id": "4rfn2ZNkZ_dG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# 1. 或者从huggingface下载数据集\n",
        "\n",
        "! huggingface-cli download --repo-type dataset --resume-download $model_id --local-dir $local_model_dir --local-dir-use-symlinks False"
      ],
      "metadata": {
        "id": "1PKPr6LxKphn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "# 2. 上传模型到modelscope\n",
        "\n",
        "! touch $local_model_dir/configuration.json\n",
        "! ls $local_model_dir\n",
        "\n",
        "from modelscope.hub.api import HubApi\n",
        "from modelscope.hub.constants import Licenses, ModelVisibility\n",
        "\n",
        "# Modelscope SDK token获取地址：https://modelscope.cn/my/myaccesstoken\n",
        "MODELSCOPE_ACCESS_TOKEN = '请输入token'\n",
        "# 请注意ModelScope平台针对SDK访问和git访问两种模式，提供两种不同的访问令牌(token)。此处请使用SDK访问令牌。\n",
        "\n",
        "api = HubApi()\n",
        "api.login(MODELSCOPE_ACCESS_TOKEN)\n",
        "model_name = model_id.replace(\"/\", \"-\")\n",
        "print(model_name)\n",
        "\n",
        "# 自己把shareAI修改成你的modelscope账户昵称\n",
        "# 创建modelscope模型仓库\n",
        "api.create_model(\n",
        "    model_id=f\"shareAI/{model_name}\",\n",
        "    visibility=ModelVisibility.PUBLIC,\n",
        "    license=Licenses.APACHE_V2,\n",
        "    chinese_name=\"push model weight\",\n",
        ")\n",
        "# 向modelscope仓库传输文件\n",
        "api.push_model(\n",
        "    model_id=f\"shareAI/{model_name}\",\n",
        "    model_dir=local_model_dir\n",
        ")"
      ],
      "metadata": {
        "id": "w_gG3dC8ZXR3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "1M5wbh7Yh_nq"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}